/**
 * Created by Alvin on 2016/11/23.
 */

$(function(){
    /**
     * 获取URL地址中属性为name的数据
     * @param name
     * @returns {*}
     * @constructor
     */
    function GetQueryString(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null) return encodeURI(r[2]);
        return null;
    }
    var smc_status = GetQueryString("smc");//获取省钱控，点击商品详情的Id

    //页面一加载，请求省钱控商品列表
    $.ajax({
        url:URL.getmoneyctrlproduct,
        type:"get",
        dataType:"json",
        data:{productid:smc_status},
        success:function(data){
            var detailHtml = template("detail_art",data);
            //动态改变标题title
            document.title=data.result[0].productName;
            $(".smc_detail_goods").html(detailHtml);

        },
        error:function(){
            alert("服务器端口错误，请及时联系管理员");
        }
    });

    //页面一加载，请求商品评论信息
    $.ajax({
        url:URL.getmoneyctrlproduct,
        type:"get",
        dataType:"json",
        data:{productid:smc_status},
        success:function(data){
            var commentHtml = template("smc_detail_comments_art",data);
            $(".smc_detail_comments").html(commentHtml);

        },
        error:function(){
            alert("服务器端口错误，请及时联系管理员");
        }
    });

    //点击页面左侧后退按钮，返回到上一页
    $(".smc_detail_header .fa-angle-left").click(function(){
        history.back(-1);
    });

    //点击返回顶部菜单，则过渡到顶部
    $(".smc_detail_return").on("click", function () {
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },15)
    });

    //给页面右下角上升火箭，注册窗口滚动事件
    window.onscroll = function(){
        //滚动窗口,动态改变火箭位置
        var arrowDis = document.body.scrollTop;
        $(".smc_detail_returnArrow").css({
            transition:"all 1s",
            position:"absolute",
            right: "20/75rem",
            bottom:""+(-arrowDis)+"px"
        })
    }
    //点击火箭，返回顶部
    $(".smc_detail_returnArrow").on("click",function(){
        var timer =setInterval(function(){
            var leader = document.body.scrollTop;
            var target = 0;
            var step = (target-leader)/10;
            step=step>0?Math.ceil(step):Math.floor(step);
            leader=leader+step;
            if(leader==target){
                clearInterval(timer);
            }
            document.body.scrollTop = leader;
        },15)
    })

})